﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pawn
{
    public partial class FrmDongLai : Form
    {
        public DongLai DL { get; set; }
        DataClassesDataContext db;
        DateTime landongtruoc;
        public Int64 TienDong
        {
            get
            {
                return tbTienDong.Value;
            }
        }
        public Int64 TienNo
        {
            get
            {
                return tbTienNo.Value;
            }
        }
        public DateTime NgayDong
        {
            get
            {
                return dpNgayDong.Value;
            }
        }
        public DateTime GiaHan
        {
            get
            {
                return dpGiaHan.Value;
            }
        }
        public FrmDongLai()
        {
            InitializeComponent();
            db = new DataClassesDataContext();

        }

        private void FrmDongLai_Load(object sender, EventArgs e)
        {
            dpNgayDong.Value = (DateTime)DL.NgayDong;
            var q = from dl in DL.PhieuCam.DongLais
                    where dl.NgayDong < dpNgayDong.Value.Date
                    orderby dl.NgayDong descending
                    select dl;
            tbNoCu.Value=0;
            landongtruoc=((DateTime)DL.PhieuCam.NgayLap).Date;
            foreach(var dl in q)
            {
                if (dl.TienDong != null && dl.TienDong != 0)
                {
                    tbNoCu.Value = (Int64)dl.TienNo;
                    landongtruoc = ((DateTime)dl.NgayDong).Date.AddDays(1);
                    break;
                }
            }
            tbLai.Value = (Int64)((dpNgayDong.Value.Date.AddDays(1) - landongtruoc).Days * DL.PhieuCam.LaiSuat * DL.PhieuCam.TienVay / 1000000);
            tbTienDong.Value = tbNoCu.Value+tbLai.Value;
            gbDongLai.Text = (dpNgayDong.Value.Date.AddDays(1) - landongtruoc).Days + " ngày";
        }

        private void tbTienDong_TextChanged(object sender, EventArgs e)
        {
            tbTienNo.Value = tbNoCu.Value + tbLai.Value - tbTienDong.Value;
        }

        private void dpNgayDong_ValueChanged(object sender, EventArgs e)
        {
            gbDongLai.Text = (dpNgayDong.Value.Date.AddDays(1) - landongtruoc).Days + " ngày";
            tbLai.Value = (Int64)((dpNgayDong.Value.Date.AddDays(1) - landongtruoc).Days * DL.PhieuCam.LaiSuat * DL.PhieuCam.TienVay / 1000000);
            dpGiaHan.Value = dpNgayDong.Value.Date.AddDays(10);
        }

        private void tbNoCu_TextChanged(object sender, EventArgs e)
        {
            tbTienDong_TextChanged(sender, e);
        }

        private void tbLai_TextChanged(object sender, EventArgs e)
        {
            tbTienDong_TextChanged(sender, e);
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (TienDong == 0)
            {
                MessageBox.Show("Bạn chưa nhập số tiền đóng!");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
